﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace TableParsers
{
    static class ClassTimeParser
    {
        private static DateTime ParseDate(string strDate, string[] possibleFormats)
        {
            DateTime date;
            FormatException lastException = null;
            foreach (string format in possibleFormats)
            {
                try
                {
                    date = DateTime.ParseExact(strDate, format, CultureInfo.InvariantCulture);
                    return date;
                }
                catch (FormatException ex)
                {
                    //ignore exception, try next format
                    lastException = ex;
                }
            }

            throw new FormatException("Failed to parse " + strDate, lastException);
        }

        public static ClassTime ParseAttendedEntry(string strDate, string strBeginTime, string strEndTime)
        {
            /* 09-03-05 or 09-3-05 or 09-03-5 or 09-3-5
             * 09:00:00 
             * 10:00:00*/


            string[] formats = { "yy-MM-dd", "yy-M-dd", "yy-MM-d", "yy-M-d" };
            DateTime date = ParseDate(strDate, formats);

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(strBeginTime, "HH:mm:ss", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(strEndTime, "HH:mm:ss", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseCancelledEntry(string strDate, string strBeginTime, string strEndTime)
        {
            /* Oct 09 2009 12:00:00:000AM or Oct 9 2009 12:00:00:000AM             
             * 09:00:00 
             * 10:00:00*/

            string[] dateParts = strDate.Split(' ');
            string strOriginalDate = dateParts[0] + " " + dateParts[1] + " " + dateParts[2];

            string[] formats = { "MMM d yyyy", "MMM dd yyyy" };
            DateTime date = ParseDate(strOriginalDate, formats);

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(strBeginTime, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(strEndTime, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseReservedEntry(string dateTimeSlot)
        {
            /*Wednesday 03/06/2009 13:00 - 14:00 or
              Wednesday 03/6/2009 13:00 - 14:00 or
              Wednesday  3/06/2009 13:00 - 14:00 or
              Wednesday  3/6/2009 13:00 - 14:00
             */

            string[] parts = dateTimeSlot.Split(' ');
            string strDate = parts[1].Trim();

            string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/MM/yyyy", "d/M/yyyy" };
            DateTime date = ParseDate(strDate, formats);

            string begin = parts[2].Trim();
            string end = parts[4].Trim();

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(begin, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(end, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }

        public static ClassTime ParseWaitListEntry(string dateTimeSlot)
        {
            /*Wednesday 09/03/2009 or 
              Wednesday 09/3/2009 or 
              Wednesday  9/03/2009 or 
              Wednesday  9/3/2009               
             */

            string[] parts = dateTimeSlot.Split(' ');

            string[] formats = { "dd/MM/yyyy", "dd/M/yyyy", "d/MM/yyyy", "d/M/yyyy" };

            DateTime date = ParseDate(parts[1], formats);
            return new ClassTime(date, new TimeSpan(), new TimeSpan());
        }

        public static ClassTime ParseBookEntry(string dateTimeSlot)
        {
            /* Nov 10 2009 12:30 - 13:30 */
            string[] parts = dateTimeSlot.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            string strDate = parts[0] + " " + parts[1] + " " + parts[2];

            string[] formats = { "MMM d yyyy", "MMM dd yyyy" };
            DateTime date = ParseDate(strDate, formats);

            string begin = parts[3].Trim();
            string end = parts[5].Trim();

            DateTime zeroDate = DateTime.Now.Date;
            DateTime beginTime = DateTime.ParseExact(begin, "HH:mm", CultureInfo.InvariantCulture);
            DateTime endTime = DateTime.ParseExact(end, "HH:mm", CultureInfo.InvariantCulture);

            return new ClassTime(date, beginTime - zeroDate, endTime - zeroDate);
        }
    }
}
